import { createRouter, createWebHashHistory } from "vue-router";
import HomeView from "../views/HomeIndex.vue";
import { useAuthStore } from '../stores/index.js'


const routeList = [
    {
        path: "/",
        redirect: "/home",
        component: () => import("../views/layout.vue"),
        children: [
            {
                path: "home",
                name: "home",
                component: HomeView,
            },
            {
                path: "dataquery",
                name: "dataquery",
                component: () => import("../views/AboutView.vue"),
                meta: { hideFooter: true }
            }, 
            {
                path: "/report",
                name: "report",
                children: [
                    {   
                        path: "productView",
                        name: "productView",
                        component: () => import("../views/ProductView.vue"),
                        meta: {
                            needDetailFooter: true // 这里会触发显示HomeFooterDetail
                          }
                    },
                    {   
                        path: "productDetail/:code",
                        name: "productDetail",
                        component: () => import("../views/productDetail.vue"),
                        meta: {
                            needicpFooter: true // 这里会触发显示HomeFooterDetail
                          }
                    },
                    {
                        path: "productDetailcharge/:code",
                      
                        name: "productDetailcharge",
                        component: () => import("../views/productDetailcharge.vue"),
                        meta: {
                            needicpFooter: true // 这里会触发显示HomeFooterDetail
                          }
                    }
                ]
            },
            // {
            //     path: "datacenter",
            //     component: () => import("../views/CaseView.vue"),
            // },
            {
                path: "AuthorServices",
                component: () => import("../views/AuthorServices.vue"),
                meta: { hideFooter: true }
            },
            {
                path: "contact",
                component: () => import("../views/NewsView.vue"),
            },
            {
                path: "personal",
                component: () => import("../views/DownloadView.vue"),
                meta: { hideFooter: true }
            },
        ],
    },
    {
        path: "/login",
        name: "login",
        component: () => import("../views/login.vue"),
    }
];

const router = createRouter({
    history: createWebHashHistory(),
    routes: routeList,
});

router.beforeEach((to, from, next) => {
    const authStore = useAuthStore()
   
    // 从 Vuex、或 localStorage 中获取 token（看你存在哪里）
    const token = authStore.token || localStorage.getItem('token')
  
    if (to.name === 'login' && token) {
      // 已登录，不允许访问登录页，直接跳到首页
      next({ name: 'home' })
    } else {
      // 其他情况一律放行
      next()
    }
  })

export default router;